<?php 
namespace App\Services;

use App\Facades\Infra\WeChatManage;

class WeChatService{

	/**
	 * [subscribe 微信关注处理]
	 * @param  [type] $userInfo [description]
	 * @return [type]           [description]
	 */
	public function subscribe($userInfo){
		$conditions['openid'] = $userInfo->openid;

		if(count(WeChatManage::getUserInfo($conditions)) == 0){
			WeChatManage::createUserInfo($this->constituteData($userInfo));
			return;
		}

		$data['subscribe'] = 1;

		WeChatManage::updateUserStatus($conditions,$data);

		return;
	}

	/**
	 * [unsubscribe 微信取消关注处理]
	 * @param  [type] $userInfo [description]
	 * @return [type]           [description]
	 */
	public function unsubscribe($userInfo){
		$conditions['openid'] = $userInfo['openid'];

		$data = $this->constituteUpdataData($userInfo);

		if(count(WeChatManage::getUserInfo($conditions)) == 0){
			$data['subscribe'] = false;
			WeChatManage::createUserInfo($data);
			return;
		}

		WeChatManage::updateUserStatus($conditions,$data);

		return;
	}

	/**
	 * [constituteData 组成数据库处理格式的数据]
	 * @param  [type] $userInfo [description]
	 * @return [type]           [description]
	 */
	public function constituteData($userInfo){
		$data = [
			'openid' => $userInfo->openid,
			'nickname' => $userInfo->nickname,
			'sex' => $userInfo->sex,
			'city' => $userInfo->city,
			'province' => $userInfo->province,
			'remark' => $userInfo->remark,
			'groupid' => $userInfo->groupid,
			'headimgurl' => $userInfo->headimgurl,
			'country' => $userInfo->country
		];

		return $data;
	}
}